package com.qf.carlikeboot.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.carlikeboot.domain.Card;
import com.qf.carlikeboot.mapper.CardMapper;
import com.qf.carlikeboot.service.CardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CardServiceImpl implements CardService {
    @Resource
    CardMapper cardMapper;

    /**
     * 添加名片
     *
     * @param card 名片对象
     * @return 影响行数
     */
    @Override
    public int addCard(Card card) {
        return cardMapper.insert(card);
    }

    /**
     * 分页获取名片列表
     *
     * @param pageIndex 第几页
     * @param pageSize  每页几条记录
     * @return 分页对象
     */
    @Override
    public PageInfo<Card> selectCardByPage(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Card> cards = cardMapper.selectAll();
        return new PageInfo<>(cards);
    }

    /**
     * 喜欢的名片列表
     *
     * @param pageIndex 第几页
     * @param pageSize  每页几条记录
     * @param liked     喜欢的名片ID集合
     * @return 分页对象
     */
    @Override
    public PageInfo<Card> selectCardByPageWithLiked(Integer pageIndex, Integer pageSize, List<String> liked) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Card> cards = cardMapper.selectByIdList(liked);
        return new PageInfo<>(cards);
    }

    /**
     * 查看名片
     *
     * @param id 名片ID
     * @return 名片对象
     */
    @Override
    public Card showCard(Integer id) {
        // 查看名片，每查看一次，浏览量加一
        Card card = cardMapper.selectByPrimaryKey(Long.valueOf(id));
        card.setVisited(card.getVisited() + 1);
        cardMapper.updateByPrimaryKeySelective(card);
        return card;
    }
}

